package cn.tedu.lbs.basic.web.service;

import cn.tedu.lbs.basic.web.repository.BusinessCategoryRepository;
import cn.tedu.lbs.basic.web.repository.BusinessRepository;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.pojo.basic.param.BusinessParam;
import com.tarena.lbs.pojo.basic.po.BusinessPO;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import com.tarena.lbs.pojo.basic.vo.BusinessCategoryVO;
import com.tarena.lbs.pojo.basic.vo.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BusinessService {
    @Autowired
    private BusinessRepository businessRepository;
    @Autowired
    private BusinessCategoryRepository businessCategoryRepository;

    public PageResult<BusinessVO> pageList(BusinessQuery businessQurey) {
        PageResult<BusinessVO> voPage=new PageResult<>(businessQurey);
        //分页查询逻辑 就是按照PageResult属性要求 挨个封装 属性值
        //但是经过 分析 pageNo pageSize就是入参值 total objects(业务数据列表) 从数据库查
        //totalPage 总页数(业务边界 谁来计算 后端计算) 100 10=10页 101 10 =11页
        //使用分页查询的插件 pageHelper
        PageInfo pageInfo = getPageResult(businessQurey);
        //1.查询total select count(*) from lbs_business where 搜索入参拼接
        //2.查询list select * from lbs_business where 搜索入参拼接 limit from,size
        voPage.setTotal(pageInfo.getTotal());
        //分页查询结果 核心list 需要转化po成vo
        List<BusinessPO> pos = pageInfo.getList();
        List<BusinessVO> vos=null;
        if (pos!=null&&pos.size()>0){
            //第一种方案 循环pos 初始化vos 每循环一个po 转化一个vo 放到vos里
            //第二种方案 stream流
            vos=pos.stream().map(po->{
                //pos每个元素 都会赋值给po变量
                //关心的映射算法 怎么把这个po转化成vo
                BusinessVO vo=new BusinessVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }

    private PageInfo getPageResult(BusinessQuery businessQurey) {
        PageHelper.startPage(businessQurey.getPageNo(),businessQurey.getPageSize());
        //直接利用query条件中非空的属性 拼接where语句 直接执行sql查询
        //select * from lbs_business
        List<BusinessPO> pos = businessRepository.list();
        log.info("分页插件开启,pos类型为:{}",pos.getClass().getName());
        //封装pageInfo对象
        PageInfo pageInfo=new PageInfo<>(pos);
        return pageInfo;
    }
    public List<BusinessCategoryVO> categoryList() {
        return businessCategoryRepository.list().stream().map(po->{
            BusinessCategoryVO vo=new BusinessCategoryVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    public void save(BusinessParam param) {
    }
}
















